﻿@using AliasVault.Client.Services.JsInterop.Models
@inject DbService DbService
@inject GlobalNotificationService GlobalNotificationService
@inject LanguageService LanguageService
@inject IStringLocalizerFactory LocalizerFactory
@using Microsoft.Extensions.Localization

<div class="relative p-6 sm:p-8 bg-white dark:bg-gray-700 rounded-lg sm:shadow-xl max-w-md w-full mx-auto">
    <div class="text-center">
        <div class="space-y-4">
            <h2 class="text-xl font-semibold text-gray-900 dark:text-white">@Localizer["UpgradeVaultTitle"]</h2>
            <p class="text-sm text-gray-500 dark:text-gray-400">
                @Localizer["UpgradeDescription"]
            </p>
            <div class="bg-gray-100 dark:bg-gray-800 p-4 rounded-lg shadow-sm">
                <div class="flex items-center justify-center mb-3">
                    <h3 class="text-lg font-medium text-gray-700 dark:text-gray-300">@Localizer["VersionInformationTitle"]</h3>
                    <button @onclick="ToggleVersionDescription" class="ml-2 w-6 h-6 bg-gray-200 dark:bg-gray-600 rounded-full flex items-center justify-center text-gray-600 dark:text-gray-400 hover:bg-gray-300 dark:hover:bg-gray-500 transition-colors">
                        <span class="text-sm font-bold">?</span>
                    </button>
                </div>

                @if (ShowVersionDescription)
                {
                    <div class="mb-4 p-3 bg-orange-50 dark:bg-orange-900/30 border border-orange-200 dark:border-orange-700 rounded-lg">
                        <p class="text-sm text-orange-800 dark:text-orange-200">
                            @string.Format(Localizer["UpgradeRequiredDescription"], LatestVersion?.Description ?? Localizer["NoDescriptionAvailable"])
                        </p>
                    </div>
                }

                <div class="space-y-2">
                    <p class="flex justify-between items-center">
                        <span class="text-sm font-medium text-gray-600 dark:text-gray-400">@Localizer["YourVaultLabel"]</span>
                        <span class="text-base font-bold text-blue-600 dark:text-blue-400">@(CurrentVersion?.CompatibleUpToVersion ?? "...")</span>
                    </p>
                    <p class="flex justify-between items-center">
                        <span class="text-sm font-medium text-gray-600 dark:text-gray-400">@Localizer["NewVersionLabel"]</span>
                        <span class="text-base font-bold text-green-600 dark:text-green-400">@(LatestVersion?.ReleaseVersion ?? "...")</span>
                    </p>
                </div>
            </div>

            <div>
                @if (ErrorMessage.Length > 0)
                {
                    <AlertMessageError Message="@ErrorMessage" />
                }

                @if (IsPendingMigrations)
                {
                    <BoldLoadingIndicator />
                }
                else
                {
                    <button @onclick="MigrateDatabase" type="button" class="px-4 mt-4 py-2 text-white bg-primary-600 rounded-lg hover:bg-primary-700 focus:ring-4 focus:ring-primary-300 dark:bg-primary-500 dark:hover:bg-primary-600 dark:focus:ring-primary-800">
                        @Localizer["StartUpgradeButton"]
                    </button>
                }
            </div>
        </div>
    </div>
</div>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Pages.Main.Sync.StatusMessages.PendingMigrations", "AliasVault.Client");
    private bool IsPendingMigrations { get; set; }
    private string ErrorMessage { get; set; } = string.Empty;
    private SqlVaultVersion? CurrentVersion { get; set; }
    private SqlVaultVersion? LatestVersion { get; set; }
    private bool ShowVersionDescription { get; set; } = false;

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // Get current and latest available database version
        CurrentVersion = await DbService.GetCurrentDatabaseVersionAsync();
        LatestVersion = await DbService.GetLatestDatabaseVersionAsync();
    }

    private void ToggleVersionDescription()
    {
        ShowVersionDescription = !ShowVersionDescription;
        StateHasChanged();
    }

    private async Task MigrateDatabase()
    {
        // Show loading indicator
        IsPendingMigrations = true;
        ErrorMessage = String.Empty;
        StateHasChanged();

        // Simulate a delay.
        await Task.Delay(1000);

        // Migrate the database
        if (await DbService.MigrateDatabaseAsync())
        {
            // Save the database to the server.
            if (await DbService.SaveDatabaseAsync())
            {
                // Migration successful, update state which will trigger the status message UI to refresh and redirect user.
                DbService.GetState().UpdateState(DbServiceState.DatabaseStatus.Ready);
                GlobalNotificationService.AddSuccessMessage(Localizer["UpgradeSuccessMessage"], true);
            }
            else
            {
                // Migration failed
                ErrorMessage = Localizer["UpgradeSuccessButSaveFailedError"];
            }
        }
        else
        {
            // Migration failed
            ErrorMessage = Localizer["UpgradeFailedError"];
        }

        // Reset local state
        IsPendingMigrations = false;
        StateHasChanged();
    }
}
